<?php
/**
 * FormValidationView
 *
 * @version    1.0
 * @package    samples
 * @subpackage tutor
 * @author     Pablo Dall'Oglio
 * @copyright  Copyright (c) 2006-2011 Adianti Solutions Ltd. (http://www.adianti.com.br)
 * @license    http://www.adianti.com.br/framework-license
 */
class FormValidationView extends TPage
{
    private $form;
    
    /**
     * Class constructor
     * Creates the page
     */
    function __construct()
    {
        parent::__construct();
        
        // create the notebook
        $notebook = new TNotebook(520, 300);
        
        // create the form
        $this->form = new TForm;
        
        // creates the notebook page
        $table = new TTable;
        
        // add the notebook inside the form
        $this->form->add($table);
        
        // adds the notebook page
        $notebook->appendPage('Form validation', $this->form);
        
        // create the form fields
        $field1 = new TEntry('field1');
        $field2 = new TEntry('field2');
        $field3 = new TEntry('field3');
        $field4 = new TEntry('field4');
        $field5 = new TEntry('field5');
        $field6 = new TEntry('field6');
        
        $field1->addValidation('Field 1', new TMinLengthValidator, array(3)); // cannot be less the 3 characters
        $field2->addValidation('Field 2', new TMaxLengthValidator, array(20)); // cannot be greater the 20 characters
        $field3->addValidation('Field 3', new TMinValueValidator, array(1)); // cannot be less the 1
        $field4->addValidation('Field 4', new TMaxValueValidator, array(10)); // cannot be greater the 10
        $field5->addValidation('Field 5', new TRequiredValidator); // required field
        $field6->addValidation('Field 6', new TEmailValidator); // email field
        
        // add a row for one field
        $row=$table->addRow();
        $row->addCell(new TLabel('Min length validator (3):'));
        $cell = $row->addCell( $field1 );
        
        // add a row for one field
        $row=$table->addRow();
        $row->addCell(new TLabel('Max length validator (20):'));
        $cell = $row->addCell( $field2 );
        
        // add a row for one field
        $row=$table->addRow();
        $row->addCell(new TLabel('Min value validator (1):'));
        $cell = $row->addCell( $field3 );
        
        // add a row for one field
        $row=$table->addRow();
        $row->addCell(new TLabel('Max value validator (10):'));
        $cell = $row->addCell( $field4 );
        
        // add a row for one field
        $row=$table->addRow();
        $row->addCell(new TLabel('Required validator:'));
        $cell = $row->addCell( $field5 );
        
        // add a row for one field
        $row=$table->addRow();
        $row->addCell(new TLabel('Email validator:'));
        $cell = $row->addCell( $field6 );
        
        // creates the action button
        $button1=new TButton('action1');
        // define the button action
        $button1->setAction(new TAction(array($this, 'onSave')), 'Save');
        $button1->setImage('ico_save.png');
        
        // add a row for the button
        $row=$table->addRow();
        $row->addCell($button1);
        
        // define wich are the form fields
        $this->form->setFields(array($field1, $field2, $field3, $field4, $field5, $field6, $button1));
        
        // add the form inside the page
        parent::add($notebook);
    }
    
    /**
     * Simulates an save button
     * Show the form content
     */
    public function onSave($param)
    {
        try
        {
            $data = $this->form->getData(); // optional parameter: active record class
            
            // form validation
            $this->form->validate();
            
            // creates a string with the form element's values
            $message = 'Field 1 : ' . $data->field1 . '<br>';
            $message.= 'Field 2 : ' . $data->field2 . '<br>';
            $message.= 'Field 3 : ' . $data->field3 . '<br>';
            $message.= 'Field 4 : ' . $data->field4 . '<br>';
            $message.= 'Field 5 : ' . $data->field5 . '<br>';
            $message.= 'Field 6 : ' . $data->field6 . '<br>';
            
            // show the message
            new TMessage('info', $message);
        }
        catch (Exception $e)
        {
            new TMessage('error', '<b>Erro</b> ' . $e->getMessage());
        }
    }
}
?>